package org.qiaice.controller;

import lombok.RequiredArgsConstructor;
import org.qiaice.result.Result;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping(value = "/admin/shop", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class ShopController {

    private final StringRedisTemplate stringRedisTemplate;

    @PutMapping(value = "/{status}")
    public Result<Object> changeStatus(@PathVariable Integer status) {
        stringRedisTemplate.opsForValue().set("SHOP_STATUS", status.toString());
        return Result.ok(null);
    }

    @GetMapping(value = "/status")
    public Result<Integer> getStatus() {
        String status = stringRedisTemplate.opsForValue().get("SHOP_STATUS");
        return Result.ok(Integer.valueOf(Objects.nonNull(status) ? status : "0"));
    }
}
